﻿using SIR.Common;
using SIR.Entities.Interfaces;
using System.Linq;

namespace SIR.Entities.Implementations
{
	/// <summary>
	/// Identifies the root most folder for a user
	/// </summary>
	public class Library : Entity, IFolder, ILibrary
	{
		/// <summary>
		/// Creates a new library instance
		/// </summary>
		public Library()
			: base()
		{
		}

		/// <summary>
		/// Creates a library object with the specified folder as the root
		/// </summary>
		/// <param name="folder">the root folder for the library</param>
		public Library(IFolder folder)
			: this()
		{
			// Set the folder reference and the user and folder Id for the library
			_folder = folder;
			FolderId = folder.Id;
			UserId = folder.UserId;
		}

		private IFolder _folder;
		/// <summary>
		/// Gets the folder that is the root folder for the library
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[Common.DatabaseAccess(DatabaseAccessRights.None)]
		public IFolder Folder
		{
			get
			{
				if (_folder == null)
				{
					_folder = DAL.DataServer.Find<Folder>(new Common.Condition.PredicateGroup(
						new[] { 
							new Common.Condition.Predicate("UserId", UserId, Common.Condition.PredicateOperator.EqualTo),
							new Common.Condition.Predicate("Id", FolderId, Common.Condition.PredicateOperator.EqualTo)
						}, true
						)).Results.FirstOrDefault();
				}

				return _folder;
			}
		}
		/// <summary>
		/// Gets or sets the name of the folder
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public string Name
		{
			get { return Folder.Name; }
			set { }
		}
		/// <summary>
		/// Gets or sets the user id for the folder
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long UserId { get; set; }
		/// <summary>
		/// Gets or sets the parent folder id for the folder
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.None)]
		public long ParentFolderId { get { return 0; } set { } }
		/// <summary>
		/// Gets the images contained within the folder
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public System.Collections.Generic.IReadOnlyCollection<IImage> Images
		{
			get { return Folder.Images; }
		}
		/// <summary>
		/// Gets the subfolders contained within the folder
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public System.Collections.Generic.IReadOnlyCollection<IFolder> Folders
		{
			get { return Folder.Folders; }
		}
		/// <summary>
		/// Gets the number of items contained within the folder
		/// </summary>
		/// <returns>The number of images</returns>
		public uint ItemsCount()
		{
			return Folder.ItemsCount();
		}
		/// <summary>
		/// Gets the number of images contained within this folder and all folders underneath it
		/// </summary>
		/// <returns>The number of images</returns>
		public uint GetNumberOfImagesInTree()
		{
			return Folder.GetNumberOfImagesInTree();
		}
		/// <summary>
		/// Gets or sets whether or not the image is set to private
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.None)]
		public bool IsPrivate
		{
			get { return Folder.IsPrivate; }
		}
		/// <summary>
		/// Gets whether ot not the parent folder is private
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.None)]
		public bool IsParentFolderPrivate
		{
			get { return Folder.IsParentFolderPrivate; }
		}
		/// <summary>
		/// Gets or sets the folder identifier for the library
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long FolderId { get; set; }
	}
}

